package com.own.component.util.excel.util.read.util;

import com.own.component.util.excel.util.read.base.BaseZipExcelReadConfig;
import com.own.component.util.excel.util.read.model.ResolveFileModel;
import lombok.extern.slf4j.Slf4j;

import java.util.List;

/**
 * ResolveFileUtil
 *
 * @author chenxueli
 * @date 2022-10-11 09:44:00
 */
@Slf4j
public class ResolveFileUtil {

    /**
     * 解析附件数据
     *
     * @param list     文件信息
     * @param config   解析配置
     * @param fileList 附件列表
     * @param <T>      泛型
     */
    public static <T> void resolve(List<T> list, BaseZipExcelReadConfig<T> config, List<ResolveFileModel> fileList) {
        // 匹配文件
        if (config.canResolveFile()) {
            list.parallelStream().forEach(item -> resolve(item, config, fileList));
        }
    }

    /**
     * 解析附件数据
     *
     * @param data     数据
     * @param config   解析配置
     * @param fileList 附件列表
     * @param <T>      泛型
     */
    public static <T> void resolve(T data, BaseZipExcelReadConfig<T> config, List<ResolveFileModel> fileList) {
        var list = fileList.stream().filter(fileModel -> config.getPatternKey().apply(data, fileModel)).toList();
        config.getSetFileKey().accept(data, list);
    }

}
